package com.jboost.modules.message.job;

import java.util.List;
import java.util.Map;
import java.util.Objects;

import cn.hutool.json.JSON;
import com.jboost.common.util.DateUtils;
import com.jboost.common.util.JSONUtil;
import com.jboost.modules.message.entity.SysMessage;
import com.jboost.modules.message.handle.ISendMsgHandle;
import com.jboost.modules.message.handle.enums.SendMsgStatusEnum;
import com.jboost.modules.message.handle.enums.SendMsgTypeEnum;
import com.jboost.modules.message.service.ISysMessageService;
import com.jboost.modules.system.util.JsonUtils;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;

/**
 * 发送消息任务
 */

@Slf4j
public class SendMsgJob implements Job {

	@Autowired
	private ISysMessageService sysMessageService;
	@Autowired
	private ApplicationContext applicationContext;

	@Override
	public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {

		log.info(String.format(" JBoost 发送消息任务 SendMsgJob !  时间:" + DateUtils.getTimestamp()));

		// 1.读取消息中心数据，只查询未发送的和发送失败不超过次数的
		QueryWrapper<SysMessage> queryWrapper = new QueryWrapper<SysMessage>();
		queryWrapper.eq("es_send_status", SendMsgStatusEnum.WAIT.getCode())
				.or(i -> i.eq("es_send_status", SendMsgStatusEnum.FAIL.getCode()).lt("es_send_num", 6));
		List<SysMessage> sysMessages = sysMessageService.list(queryWrapper);
		System.out.println(sysMessages);
		// 2.根据不同的类型走不通的发送实现类
		for (SysMessage sysMessage : sysMessages) {
			ISendMsgHandle sendMsgHandle = null;
			try {
				sendMsgHandle = (ISendMsgHandle) applicationContext.getBean(Class.forName(SendMsgTypeEnum.getByType(sysMessage.getEsType()).getImplClass()));
			} catch (Exception e) {
				log.error(e.getMessage(),e);
			}
			Integer sendNum = sysMessage.getEsSendNum();
			try {
				if (Objects.equals(sysMessage.getEsType(),SendMsgTypeEnum.APP.getType())){
					sendMsgHandle.SendMsg(sysMessage.getEsReceiver(), sysMessage.getEsTitle(),
							sysMessage.getEsContent().toString(), JSONUtil.fromJson(sysMessage.getEsParam(), Map.class),JSONUtil.fromJson(sysMessage.getRemark(),Object.class));
				}else {
					sendMsgHandle.SendMsg(sysMessage.getEsReceiver(), sysMessage.getEsTitle(),
							sysMessage.getEsContent().toString());
				}
				// 发送消息成功
				sysMessage.setEsSendStatus(SendMsgStatusEnum.SUCCESS.getCode());
			} catch (Exception e) {
				// 发送消息出现异常
				sysMessage.setEsSendStatus(SendMsgStatusEnum.FAIL.getCode());
			}
			sysMessage.setEsSendNum(++sendNum);
			// 发送结果回写到数据库
			sysMessageService.updateById(sysMessage);
		}

	}

}
